package day12;

import java.io.*;
import java.util.Scanner;

/**
 * @author Xiao
 * @date 2021/8/3
 */
public class Test01 {
    /**
     * 1. 键盘录入数据源路径和目的地路径，完成对指定文件的复制
     *     例如：
     *         数据源： d:\a.txt
     *         目的地： d:\b.txt
     *
     *         就将a.txt文件的数据复制到b.txt中
     */
    public static void main(String[] args) throws IOException {
        /*BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                "homework\\src\\day12\\file01a.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
                "homework\\src\\day12\\file01b.txt"));*/
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数据源路径：");
        File file1 = getpath(sc);
        System.out.println("请输入目的地路径：");
        File file2 = getpath(sc);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
        int value;
        while ((value = bis.read()) != -1) {
            bos.write(value);
        }
        bis.close();
        bos.close();
    }

    /**判断键盘录入的文件是否存在*/
    private static File getpath(Scanner sc) {
        while (true) {
            String path = sc.nextLine();
            File file = new File(path);
            if (file.isFile()) {
                return file;
            }
            System.out.println("您输入的路径有误，请重新输入：");
        }
    }
}
